package com.wangshb.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshb.upms.core.constants.NumConstant;
import com.wangshb.upms.core.exception.BusinessException;
import com.wangshb.upms.core.exception.OptimisticLockException;
import com.wangshb.upms.core.tree.TreeNode;
import com.wangshb.upms.core.utils.AssertUtil;
import com.wangshb.upms.entity.UpmsPermission;
import com.wangshb.upms.mapper.UpmsPermissionMapper;
import com.wangshb.upms.service.UpmsPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import  com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UpmsPermissionServiceImpl extends ServiceImpl<UpmsPermissionMapper, UpmsPermission> implements UpmsPermissionService {

    @Resource
    private UpmsPermissionMapper upmsPermissionMapper;

    @Override
    public Page<UpmsPermission> findPage(int pageNo, int pageSize, UpmsPermission condition) {

        Page<UpmsPermission> page = new Page<>(pageNo, pageSize, true);
        page.setRecords(findList(page, condition));
        return page;
    }

    @Override
    public List<UpmsPermission> findList(Page<UpmsPermission> page, UpmsPermission condition) {
        return this.upmsPermissionMapper.findList(page, condition);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addUpmsPermission(UpmsPermission upmsPermission) {

        // 查看用户的登陆账号是否存在
        UpmsPermission dbUpmsPermission = this.findOne(UpmsPermission.builder().permissionCode(upmsPermission.getPermissionCode()).build());
        AssertUtil.isNull(dbUpmsPermission, "权限编码已经存在！");

        this.upmsPermissionMapper.insert(upmsPermission);
        return upmsPermission.getId();
    }

    @Override
    public UpmsPermission findOne(UpmsPermission upmsPermission) {
        QueryWrapper<UpmsPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(upmsPermission);
        return this.upmsPermissionMapper.selectOne(queryWrapper);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delUpmsPermission(UpmsPermission upmsPermission) {
        UpmsPermission delEntity = new UpmsPermission();
        delEntity.setId(upmsPermission.getId());
        delEntity.setVersion(upmsPermission.getVersion());
        delEntity.setDeleted(NumConstant.ONE);
        updateUpmsPermissionWithLock(delEntity);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUpmsPermissionWithLock(UpmsPermission upmsPermission) {

        AssertUtil.notNull(upmsPermission, "权限信息不能为空");
        AssertUtil.notNull(upmsPermission.getVersion(), "版本号不能为空");

        // 如果角色编码不为空，需要校验角色编码是否已经存在
        if (StringUtils.isNotBlank(upmsPermission.getPermissionCode())) {
            UpmsPermission dbUpmsPermission = this.findOne(UpmsPermission.builder().permissionCode(upmsPermission.getPermissionCode()).build());
            AssertUtil.isTrue(null == dbUpmsPermission || dbUpmsPermission.getId().equals(dbUpmsPermission.getId()), "权限编码已经存在！");
        }

        if (!this.updateById(upmsPermission)) {
            throw new OptimisticLockException("数据已被修改");
        }
    }

    @Override
    public List<TreeNode> findForPermissionTree() {
        return this.upmsPermissionMapper.findForPermissionTree();
    }
}
